package com.gewu.advertisement.core.model

import android.util.Log
import com.fasterxml.jackson.annotation.JsonProperty
import com.gewu.advertisement.core.model.Advertising.Type.*
import java.io.Serializable
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author htt 2021/1/7
 */
data class Advertising(
    @JsonProperty("adId")
    val id: Int? = null,
    val adName: String = "",
    val adType: Type? = null,
    val adLink: String = "",
    val adText: String = "",
    val expireTime: String = "",                        //到期时间 yyyy-MM-dd HH:mm:ss
    val imageStayTime: Int = 0,                         //图片停留时间 单位秒
    val textRollPosition: TextRollPosition? = null,     //文字滚动位置
    val textRollDirection: TextRollDirection? = null,   //文字滚动方向
    val adTextColor: String = "FFFFFF",                 //文字颜色
    val videoPlayDuration: Long = 0L,                   //视频播放时长 单位秒 默认:0 一直播放完
    val videoPlayTimes: Int = 0,                        //视频循环播放次数
    val textRollSpeed: Int = 12,                        //文字滚动速度
    val textSize: Int = 20,                             //文字大小
    val adHashCode: String = "",                        //广告资源HashCode
    val webUrl:String = "",                             //HTML网页地址
    val webStayTime: Int = 0,                            //图片停留时间 单位秒
    val adTextBgColor: String = "FFF000",                 //文字广告背景颜色
    val startTime: String? = null,                       //播放时间范围 ： 开始时间
    val endTime: String? = null                          //播放时间范围 ： 结束时间

) : Serializable {


    /**
     * 这个广告是否已经过期
     */
    val isExpire: Boolean
        get() = if (expireTime.isBlank()) false else expireTimeMilli <= 0


    /**
     * 过期时间 单位毫秒
     */
    val expireTimeMilli: Long
        get() {
            if (expireTime.isBlank()) return Long.MAX_VALUE
            val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
            return try {
                formatter.parse(expireTime)!!.time - System.currentTimeMillis()
            } catch (e: Exception) {
                Log.e("Advertising", "到期时间解析失败:$this")
                0
            }
        }


    override fun toString(): String {
        return when (adType) {
            IMAGE -> "Advertising(id=$id, adName='$adName', adType=$adType, expireTime='$expireTime', imageStayTime=$imageStayTime,startTime='$startTime', endTime='$endTime')"
            TEXT -> "Advertising(id=$id, adName='$adName', adType=$adType, adText='$adText', expireTime='$expireTime', textRollPosition=$textRollPosition, textRollDirection=$textRollDirection, textRollSpeed=$textRollSpeed, textSize=$textSize,adTextColor=$adTextColor,adTextBgColor=$adTextBgColor ,startTime='$startTime', endTime='$endTime')"
            VIDEO -> "Advertising(id=$id, adName='$adName', adType=$adType, expireTime='$expireTime', videoPlayDuration=$videoPlayDuration, videoPlayTimes=$videoPlayTimes,startTime='$startTime', endTime='$endTime')"
            HTML -> "Advertising(id=$id, adName='$adName', adType=$adType, expireTime='$expireTime', webStayTime=$webStayTime, webUrl=$webUrl,startTime='$startTime', endTime='$endTime')"
            null -> "Advertising(id=$id, adName='$adName', adType=$adType, adLink='$adLink', adText='$adText', expireTime='$expireTime', imageStayTime=$imageStayTime, textRollPosition=$textRollPosition, textRollDirection=$textRollDirection, videoPlayDuration=$videoPlayDuration, videoPlayTimes=$videoPlayTimes, textRollSpeed=$textRollSpeed, textSize=$textSize,startTime='$startTime', endTime='$endTime')"
        }
    }

    /**
     * 广告类型
     */
    enum class Type {
        IMAGE,
        TEXT,
        VIDEO,
        HTML
    }

    /**
     * 文字滚动方向
     */
    enum class TextRollDirection {
        LEFT_TO_RIGHT,
        RIGHT_TO_LEFT
    }

    /**
     * 文字滚动位置
     */
    enum class TextRollPosition {
        BOTTOM,
        TOP,
        CENTER
    }


}

